

export const changeMoneyUnit1 = (num) => {
	// if (Number(num) <= 1) return {
	// 	num,
	// 	unit: '元'
	// }
	if (Number(num) <= 1) return 0
	var moneyUnits = ["", "万", "亿", "万亿"]
	var dividend = 10000
	var curentNum = num
	//转换数字 
	var curentUnit = moneyUnits[0]
	//转换单位 
	for (var i = 0; i < 4; i++) {
		curentUnit = moneyUnits[i]
		if (strNumSize(curentNum) < 5) {
			break
		}
		curentNum = curentNum / dividend
	}
	var m = {
		num: 0,
		unit: ""
	}
	m.num = Number(curentNum).toFixed(2)
	m.unit = curentUnit
	let str = m.num + m.unit
	return str
	// return m
}

const strNumSize = (tempNum) => {
	var stringNum = tempNum.toString()
	var index = stringNum.indexOf(".")
	var newNum = stringNum
	if (index != -1) {
		newNum = stringNum.substring(0, index)
	}
	return newNum.length
}

export const changeMoneyUnit = (value) => {
	const newValue = { num: '', unit: '' }
	let fr = 1000
	let num = 3
	let text1 = ''
	let fm = 1
	while (value / fr >= 1) {
		fr *= 10
		num += 1
		// console.log('数字', value / fr, 'num:', num)
	}
	if (num <= 4) { // 千
		newValue.num = parseInt(value / 1000) + ''
		newValue.unit = '千元'
	} else if (num <= 8) { // 万
		text1 = parseInt(num - 4) / 3 > 1 ? '千万元' : '万元'
		// tslint:disable-next-line:no-shadowed-variable
		fm = text1 === '万元' ? 10000 : 10000000
		if (value % fm === 0) {
			newValue.num = parseInt(value / fm) + ''
		} else {
			newValue.num = parseFloat(value / fm).toFixed(2) + ''
		}
		newValue.unit = text1
	} else if (num <= 16) { // 亿
		text1 = (num - 8) / 3 > 1 ? '千亿元' : '亿元'
		text1 = (num - 8) / 4 > 1 ? '万亿元' : text1
		text1 = (num - 8) / 7 > 1 ? '千万亿元' : text1
		// tslint:disable-next-line:no-shadowed-variable
		fm = 1
		if (text1 === '亿元') {
			fm = 100000000
		} else if (text1 === '千亿元') {
			fm = 100000000000
		} else if (text1 === '万亿元') {
			fm = 1000000000000
		} else if (text1 === '千万亿元') {
			fm = 1000000000000000
		}
		if (value % fm === 0) {
			newValue.num = parseInt(value / fm) + ''
		} else {
			newValue.num = parseFloat(value / fm).toFixed(2) + ''
		}
		newValue.unit = text1
	}
	if (value < 1000) {
		newValue.num = value + ''
		newValue.unit = ''
	}
	return newValue
}


